﻿using System.Threading.Tasks;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Generic;
using System.Drawing;
using System;
using System.Windows;
using NetCore.PdfPreViewLib.Services;
using NetCore.PdfPreViewLib.Helper;

namespace PdfPreView.ViewModels
{
    public class MainWindowVM : BindableBase
    {
        string rootPath;

        string pdfPath1;

        string pdfPath2;

        List<Bitmap> bitmaps;

        public MainWindowVM()
        {
            var path = System.Environment.CurrentDirectory;
            for (int i = 0; i < 3; i++)
            {
                path = System.IO.Path.GetDirectoryName(path);
            }
            rootPath = path + @"\Resource\";

            pdfPath1 = rootPath + @"target.pdf";

            pdfPath2 = rootPath + @"testm.pdf";

        }

        private string pdfPath;

        public string PdfPath
        {
            get { return pdfPath; }
            set { SetProperty(ref pdfPath, value); }
        }

        private DelegateCommand<object> pdfLoadedCommand;
        public DelegateCommand<object> PdfLoadedCommand => pdfLoadedCommand ?? (pdfLoadedCommand = new DelegateCommand<object>(ExecutePdfLoadedCommand));

        private DelegateCommand printPdfCommand;
        public DelegateCommand PrintPdfCommand => printPdfCommand ?? (printPdfCommand = new DelegateCommand(ExcutePrintPdfCommand));

        private DelegateCommand _clickCommnd;
        public DelegateCommand ClickCommnd => _clickCommnd ??= new DelegateCommand(ExecuteClickCommnd);

        private void ExecutePdfLoadedCommand(object obj)
        {
            if (obj is NetCore.PdfPreViewLib.MoonPdfPanel)
            {
                var target = obj as NetCore.PdfPreViewLib.MoonPdfPanel;
                if (target != null)
                {
                    bitmaps = new List<Bitmap>();
                    for (int i = 0; i < target.TotalPages; i++)
                    {
                        var bitmap = MuPdfWrapper.ExtractPage(new FileSource(PdfPath), i, 3.0f,null);
                        bitmaps.Add(bitmap);
                    }
                }
            }
            GC.Collect();
            GC.WaitForFullGCComplete();
        }
        private void ExcutePrintPdfCommand()
        {
            //_ = PrintPdfAsync();

            var factory = Task.Factory.StartNew(PdfPrintFunByLocal).ContinueWith(task =>
              {
                  if (task.Result) MessageBox.Show("打印成功！");
                  else
                  {
                      MessageBox.Show("打印失败，请到窗口打印！");
                  }

                  if (task.IsCompleted) task.Dispose();
              });
            GC.Collect();
            GC.WaitForFullGCComplete();
        }
        
        private bool PdfPrintFunByLocal()
        {
            try
            {
                var print = new PrintImageHelper();
                print.bitmaps = bitmaps;
                print.PrintBitmap();
                return print.isSucPrint;
            }
            catch { return false; }
        }

        private void ExecuteClickCommnd()
        {
            if (PdfPath == null) PdfPath = pdfPath1;
            else if (PdfPath == pdfPath1) PdfPath = pdfPath2;
            else PdfPath = pdfPath1;
        }
    }
}
